/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_STATE_GRID_A_H
#define MK_STATE_GRID_A_H
#include "MkSip/MkSipServer.h"
#include "MkStateGridADataType.h"
#include "MkUtil/MkRwLock.h"

typedef MkFunction<void(const MkSipStatus& StatusCode, const CMkStateGridAResponseBase* pResponse)> MkStateGridAResponseCallback;
typedef MkFunction <Uint32(const CMkStateGridARequestBase* pRequest, const MkStateGridAResponseCallback& ResCb)> MkStateGridARequestCallback;
class MkApi CMkStateGridA : public CMkSipServer
{
    typedef struct _request_param
    {
        MkStateGridAResponseCallback        ResCb;
        CMkTime                             RequestTime;
        MkString                            StrKey;
    }MkSGARequestParam;
public:
    CMkStateGridA(CMkEventPool* pEventPool, CMkTimer* pTimer);
    virtual ~CMkStateGridA();

    Uint32 Start(const MkString& LocalSipId, const MkString& Realm, const MkString& LocalHost, Uint16 Port, const MkStateGridARequestCallback& ReqCb);
    Uint32 Stop();

    Uint32 RealPlay(const MkString& RemoteId, const MkString& TargetId, BOOL bSubStream, const MkString& RemoteRealm, const MkString& RemoteIp, Uint16 RemotePort, const MkString& RtpHost, Uint16 RtpPort, const MkStateGridAResponseCallback& ResCb);
    //cmd from page 41-42
    Uint32 PtzControl(const MkString& RemoteId, const MkString& TargetId, const MkString& RemoteRealm, const MkString& RemoteIp, Uint16 RemotePort, Uint32 Cmd, Uint32 Param1, Uint32 Param2, Uint32 Param3, const MkStateGridAResponseCallback& ResCb);
    Uint32 Ptz3DZoom(const MkString& RemoteId, const MkString& TargetId, const MkString& RemoteRealm, const MkString& RemoteIp, Uint16 RemotePort, Uint32 Cmd, Uint32 Param1, Uint32 Param2, Uint32 Param3, Uint32 Param4, Uint32 Param5, Uint32 Param6, Uint32 Param7, const MkStateGridAResponseCallback& ResCb);
    Uint32 AlarmSubscribe(const MkString& RemoteId, const MkString& TargetId, const MkString& RemoteRealm, const MkString& RemoteIp, Uint16 RemotePort, Uint32 Expries, const MkMap<MkString, Uint32>& MapSubId, const MkStateGridAResponseCallback& ResCb);
    Uint32 StatusSubscribe(const MkString& RemoteId, const MkString& TargetId, const MkString& RemoteRealm, const MkString& RemoteIp, Uint16 RemotePort, Uint32 Expries, const MkVectorString& VecSubId, const MkStateGridAResponseCallback& ResCb);

    Uint32 StopAlarmSubscirbe(const MkString& SubscribeId, const MkStateGridAResponseCallback& ResCb);
    Uint32 StopStatusSubscirbe(const MkString& SubscribeId, const MkStateGridAResponseCallback& ResCb);
    Uint32 StopRealPlay(const MkString& SessionId, const MkStateGridAResponseCallback& ResCb);

    Uint32 AlarmNotify(const MkString& SubId, const MkString& StrState, const MkString& Code, const MkString& Name, const Uint32 AlarmType, const CMkTime& StartTime, const CMkTime& EndTime, const MkStateGridAResponseCallback& ResCb);
    Uint32 StateNotify(const MkString& SubId, const MkString& StrState, const MkString& Code, const MkString& Name, Uint32 Status, const MkStateGridAResponseCallback& ResCb);
protected:
    Uint32 OnSipRequest(const MkSipRequestEvent& RequestEvent, const CMkSipMessage& SipRequest, const EventRequestCallback& Cb) override;
    Uint32 OnSipResponse(const MkSipResponseEvent& ResponseEvent, const MkString& TransActionId, const CMkSipMessage& SipRequest, const CMkSipMessage& SipResponse, const EventResponseCallback& Cb) override;

private:
    Uint32 MessageRequestParse(const CMkSipMessage& SipRequest, const EventRequestCallback& Cb);
    Uint32 SubscribeRequestParse(const CMkSipMessage& SipRequest, const EventRequestCallback& Cb);
    Uint32 InviteRequestParse(const CMkSipMessage& SipRequest, const EventRequestCallback& Cb);
    Uint32 SubscribeNotifyParse(const CMkSipMessage& SipRequest, const EventRequestCallback& Cb);
    Uint32 ByeParse(const CMkSipMessage& SipRequest, const EventRequestCallback& Cb);
    Uint32 InviteResponseParse(const CMkSipMessage& SipRequest, const CMkSipMessage& SipResponse, const EventResponseCallback& Cb);

    Uint32 PtzControlRequestParse(const MkString& ChannelId, MkStateGridPtzCommandType Cmd, Uint32 Param1, Uint32 Param2, Uint32 Param3, Uint32 Param4, Uint32 Param5, Uint32 Param6, Uint32 Param7, const EventRequestCallback& EventCallback);

    Uint32 NotifyAlarmRequestParse(const MkString& StrState, const MkString& XmlBody, const EventRequestCallback& EventCallback);
    Uint32 NotifyStatusRequst(const MkString& StrState, const MkString& XmlBody, const EventRequestCallback& EventCallback);

    Uint32 ResponseCallback(const MkSipStatus& StatusCode, const MkString& strKey, const CMkStateGridAResponseBase* pRes);
    Uint32 AddRequestToMap(const MkString& Key, const MkStateGridAResponseCallback& ResCb);
private:
    MkStateGridARequestCallback              m_ReqCb;
    CMkRwLock                                m_ReqLock;
    MkMap<MkString, MkSGARequestParam>       m_MapRequst;
};




#endif
